﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kendo.Mvc.UI;

namespace Admin.Controllers
{
    public class CommonController : Controller
    {
        #region 图片上传
        public ActionResult ImageSave(HttpPostedFileBase Image)
        {
            // The Name of the Upload component is "files"
            if (Image != null)
            {
                // Some browsers send file names with full path.
                // We are only interested in the file name.
                string fileExtension = "." + Image.FileName.Split('.').LastOrDefault();
                var oppFilename = Guid.NewGuid() + fileExtension;
                Image.SaveAs(Path.Combine(Request.MapPath("~/Upload/DoctorImages"),
                    oppFilename));


                //return Content("");
                return Json(new { success = true, fileName = oppFilename });
            }

            // Return an empty string to signify success
            return Content("error");
        }

        public ActionResult ImageRemove(string[] fileNames)
        {
            // The parameter of the Remove action must be called "fileNames"
            if (fileNames != null)
            {
                foreach (var fullName in fileNames)
                {
                    var fileName = Path.GetFileName(fullName);
                    if (fileName != null)
                    {
                        var physicalPath = Path.Combine(Server.MapPath("~/Upload/DoctorImages"), fileName);

                        // TODO: Verify user permissions

                        if (System.IO.File.Exists(physicalPath))
                        {
                            // The files are not actually removed in this demo
                            System.IO.File.Delete(physicalPath);
                        }
                    }
                }
            }

            // Return an empty string to signify success
            return Content("");
        }
        #endregion

    }

}